package com.hebeu.device.scheduled;


import com.hebeu.device.dao.DMaintenanceMapper;
import com.hebeu.device.dao.DServiceMapper;
import com.hebeu.device.dao.DUserMapper;
import com.hebeu.device.pojo.*;
import com.hebeu.device.service.DMaintenanceService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 维修记录定时任务
 * 定时将未分配的维修记录随机分配给维修人员
 */
@Service
public class DMaintenanceScheduledTask {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");

    @Resource
    private DMaintenanceMapper dMaintenanceMapper;
    @Resource
    private DUserMapper dUserMapper;
    @Resource
    private DServiceMapper dServiceMapper;

    /**
     * 每隔一小时执行一次
     */
    @Scheduled(fixedRate = 3600000)
    public void reportCurrentTime(){
        System.out.println("自动分配任务开始执行-----时间："+DATE_FORMAT.format(new Date()));

        DServiceExample dServiceExample = new DServiceExample();
        DServiceExample.Criteria criteria = dServiceExample.createCriteria();
        //插入查询条件：未分配的报修记录
        criteria.andSStatusEqualTo(1);
        //执行查询语句
        List<DService> dServiceList = dServiceMapper.selectByExample(dServiceExample);
        //判断报修记录是否为空
        if (dServiceList.size()!=0){
            //查询维修人员
            DUserExample dUserExample = new DUserExample();
            DUserExample.Criteria criteriaDUser = dUserExample.createCriteria();
            criteriaDUser.andUTypeEqualTo(3);
            List<DUser> dUserList = dUserMapper.selectByExample(dUserExample);
            if (dUserList.size()!=0){
                for (DService dService : dServiceList){
                    //随机数
                    Random random = new Random();
                    int r = random.nextInt(dUserList.size());
                    //选取维修人员
                    DUser dUser = dUserList.get(r);
                    //创建维修记录对象
                    DMaintenance dMaintenance = new DMaintenance();
                    //维修延迟程度
                    dMaintenance.setPostponeDegree(1);
                    //初始化人工费
                    dMaintenance.setPersonPay(0.0);
                    //初始化材料费
                    dMaintenance.setMatiPay(0.0);
                    //创建时间
                    dMaintenance.setCreateTime(new Date());
                    //绑定报修记录id
                    dMaintenance.setServiceId(dService.getsId());
                    //已分配状态
                    dMaintenance.setMainStatus(2);
                    //分配维修人员id
                    dMaintenance.setMainPerson(dUser.getuId());
                    //分配时间
                    dMaintenance.setDisTime(new Date());
                    //最后修改时间
                    dMaintenance.setAlterTime(new Date());
                    try {
                        //新增维修记录
                        dMaintenanceMapper.insertSelective(dMaintenance);
                        //更新报修记录状态为正在进行
                        dService.setsStatus(2);
                        dServiceMapper.updateByPrimaryKeySelective(dService);
                        System.out.println("执行成功:编号为"+dService.getsId()+"的报修记录已分配给<"+dUser.getUsername()+">维修员");
                    }catch (Exception e){
                        e.printStackTrace();
                        throw new RuntimeException("执行失败:修改维修记录失败！");
                    }
                }


            }else {
                System.out.println("执行成功:没有可用维修人员！");
            }
        }else {
            System.out.println("执行成功:没有正在进行的报修记录！");
        }


    }

}
